#用户-物品，用户数>物品数
user_movie_ratings = {
    '捷荣技术': {'电子原件': 5, '华为': 3, '消费电子': 4, '芯片': 4},
    '华映科技': {'面板': 3, '华为': 1, '消费电子': 5, '苹果': 4},
    '荣联科技': {'面板': 4, '星闪': 2, '芯片': 4, '华为': 4},
    '冠石科技': {'芯片': 5, '面板': 3, '华为': 4,'消费电子': 5},
}

#物品-用户，物品数>用户数
user_movie_ratings2 = {
    '消费电子': {'华映科技': 5, '捷荣技术': 3, '荣联科技': 4, '宁德时代': 4, '远东传动': 4},
    '新题材': { '捷荣技术': 1, '荣联科技': 1, '宁德时代': 1,'华映科技': 1, '林州重工': 1},
    '面板': {'瑞贝卡': 3, '华映科技': 1, '荣联科技': 5, '深科技': 4},
    '星闪': {'荣联科技': 4, '瑞贝卡': 2},
    '华为': {'荣联科技': 5, '华映科技': 3, '捷荣技术': 4,'冠石科技': 5},
    '苹果': {'华映科技': 3},
}

user_movie_ratings3 = {}
user_movie_ratings3['消费电子']={'华映科技': 5, '捷荣技术': 3, '荣联科技': 4, '宁德时代': 4, '远东传动': 4}
user_movie_ratings3['新题材']={'华映科技': 1, '捷荣技术': 1, '荣联科技': 1, '宁德时代': 1, '林州重工': 1}
user_movie_ratings3['面板']={'瑞贝卡': 3, '华映科技': 1, '荣联科技': 5, '深科技': 4}
user_movie_ratings3['星闪']={'荣联科技': 4, '瑞贝卡': 2}
user_movie_ratings3['华为']= {'荣联科技': 5, '华映科技': 3, '捷荣技术': 4,'冠石科技': 5}
user_movie_ratings3['苹果']= {'华映科技': 3}


user_movie_ratings4 = {}
user_movie_ratings4['华映科技']={'捷荣技术': 3, '荣联科技': 4, '宁德时代': 4, '远东传动': 4}
user_movie_ratings4['捷荣技术']={'华映科技': 1, '远东传动': 1, '荣联科技': 1, '宁德时代': 1, '林州重工': 1}
user_movie_ratings4['瑞贝卡']={'华映科技': 1, '深科技': 4}
user_movie_ratings4['荣联科技']={'林州重工': 4, '瑞贝卡': 2}
user_movie_ratings4['冠石科技']= {'荣联科技': 5, '华映科技': 3, '捷荣技术': 4}
user_movie_ratings4['远东传动']= {'华映科技': 3}


import math
import pandas
import csv
import json

def make_json(csvFilePath):
    data = {}
    with open(csvFilePath, encoding='utf-8') as csvf:
        csvReader = csv.DictReader(csvf)
        # Convert each row into a dictionary
        # and add it to data
        for rows in csvReader:
            # Assuming a column named 'No' to
            # be the primary key
            key = rows['name']
            data[key] = rows
    # Open a json writer, and use the json.dumps()
    # function to dump data
    # with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
    #     jsonf.write(json.dumps(data, indent=4))

    result = json.dumps(data, indent=4)
    print(result)
    return result

# Driver Code
# Decide the two file paths according to your
# computer system
# csvFilePath = r'Names.csv'
# jsonFilePath = r'Names.json'
# # Call the make_json function
# make_json(csvFilePath, jsonFilePath)


def cosine_similarity(a, b):
    numerator = sum([a[key] * b[key] for key in a if key in b])
    denominator = math.sqrt(sum([a[key]**2 for key in a])) * math.sqrt(sum([b[key]**2 for key in b]))
    return numerator / denominator

def recommend_movies(user, user_movie_ratings):
    # 计算目标用户与其他用户的相似度
    similarities = {other_user: cosine_similarity(user_movie_ratings[user], user_movie_ratings[other_user]) for other_user in user_movie_ratings if other_user != user}

    # 按相似度降序排列用户
    sorted_users = sorted(similarities, key=similarities.get, reverse=True)

    # 找到与目标用户最相似的用户
    most_similar_user = sorted_users[0]

    # 找到最相似用户喜欢的电影，但目标用户未看过的电影
    recommended_movies = {movie: rating for movie, rating in user_movie_ratings[most_similar_user].items() if movie not in user_movie_ratings[user]}

    print(similarities)
    print(most_similar_user)

    # 按评分降序排列推荐电影
    sorted_recommended_movies = sorted(recommended_movies, key=recommended_movies.get, reverse=True)

    return sorted_recommended_movies

# 为Alice推荐电影
# data  = make_json(r"D:\work\__WorkPlace\pythonWork\test.csv")
# recommended_movies = recommend_movies('远东传动', user_movie_ratings4)
# print(f"Recommended movies for Alice: {recommended_movies}")

with open('d:/a.txt', 'w') as f:
    json.dump(user_movie_ratings4,f)

with open('d:/a.txt', 'r') as f:
    data = json.load(f)
    print(data)
# data  = pandas.read_csv(r"D:\work\__WorkPlace\pythonWork\test.csv",encoding="utf8")
# preference = data.pivot_table(index="value",columns="name",values="base")
# print(user_movie_ratings2)

# print(user_movie_ratings3)
